//
//  ByNetProductDetailModel.m
//  hutouben_ios
//
//  Created by pro on 16/11/30.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByModelProductDetail.h"

#import "SkuIdModel.h"
#import "SkuNameModel.h"
#import "SkuModel.h"
@implementation ByModelProductDetail

- (void)setAttributes:(NSDictionary *)jsonDic {
    
    [super setAttributes:jsonDic];
    
    self.main_img = BOYE_PICTURE_VIEW_URL(jsonDic[@"main_img"]);
    self.title = self.name;
    self.desc = self.synopsis;
    self.postage = @"包邮";
    
    NSMutableArray *imgs = [[NSMutableArray alloc] init];
    for (NSString *imgStr in jsonDic[@"carousel_images"]) {
        [imgs addObject:BOYE_PICTURE_VIEW_URL(imgStr)];
    }
    self.imgs = imgs.copy;
    
    // 多规格设置
    NSMutableArray *sku_nameList  = [[NSMutableArray alloc] init];
    NSMutableArray *sku_idList    = [[NSMutableArray alloc] init];
    NSArray *sku_info = jsonDic[@"sku_info"];
    for (NSDictionary *sku_dic in sku_info) {
        SkuNameModel    *nameModel  = [[SkuNameModel alloc] init];
        SkuIdModel      *idModel    = [[SkuIdModel alloc] init];
        
        nameModel.sid_des   = sku_dic[@"sku_name"];
        idModel.sid         = sku_dic[@"sku_id"];
        
        NSMutableArray *v_nameList  = [[NSMutableArray alloc] init];
        NSMutableArray *v_idList    = [[NSMutableArray alloc] init];
        NSArray *value_list = sku_dic[@"value_list"];
        
        for (NSDictionary *value_dic in value_list) {
            [v_nameList addObject:value_dic[@"value_name"]];
            [v_idList   addObject:value_dic[@"value_id"]];
        }
        
        nameModel.vidAry_des    = v_nameList.copy;
        idModel.vidAry          = v_idList.copy;
        
        // 添加模型
        [sku_nameList addObject:nameModel];
        [sku_idList addObject:idModel];
    }
    
    self.skuIdAry   = sku_idList.copy;
    self.skuNameAry = sku_nameList.copy;
    
    CGFloat minPirce = HUGE_VALF;
    CGFloat maxOriPirce = 0;
    NSInteger quantity = 0;
    NSArray *sku_list = jsonDic[@"sku_list"];
    NSMutableArray *skuModels = [[NSMutableArray alloc] init];
    for (NSDictionary *skuDic in sku_list) {
        SkuModel *model = [[SkuModel alloc] initContentWithDic:skuDic];
        [skuModels addObject:model];
        if (model.price.floatValue < minPirce) {
            minPirce = model.price.floatValue;
        }
        if (model.ori_price.floatValue > maxOriPirce) {
            maxOriPirce = model.ori_price.floatValue;
        }
        quantity += model.quantity.integerValue;
    }
  
    for (int i = 0; i < self.skuIdAry.count; i ++) {
        
    }
    self.skuAry = skuModels.copy;

    // 库存
    self.quantity = [NSString stringWithFormat:@"%li", quantity];
    
    // 价格
    self.price = @(minPirce);
    self.oriPrice = @(maxOriPirce);


    // 参数
    NSMutableArray *params = [[NSMutableArray alloc] init];
    NSArray *properties = jsonDic[@"properties"];
    for (NSDictionary *p_dic in properties) {
        NSString *prop_name = p_dic[@"prop_name"];
        NSArray *prop_value_list = p_dic[@"prop_value_list"];
        NSString *value_name = ([prop_value_list firstObject])[@"value_name"];
        [params addObject:[NSString stringWithFormat:@"%@:%@", prop_name, value_name]];
    }
    self.params = params.copy;
    

}

- (void)operationSkuAry {
    
//    SkuIdModel *idModel
}

@end
